package com.salton123.tone

import android.view.KeyEvent
import android.view.View
import android.view.inputmethod.EditorInfo
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import com.alibaba.android.arouter.facade.annotation.Route
import com.salton123.soulove.common.Constants
import com.salton123.soulove.lib_demo.R
import com.salton123.tone.db.FundAppDatabase
import com.salton123.tone.mvvm.FundationViewModel
import com.salton123.tone.util.AssetsUtils
import com.salton123.ui.base.BaseActivity
import kotlinx.android.synthetic.main.search_title.*

/**
 * User: wujinsheng1@yy.com
 * Date: 2020/9/4 18:05
 * ModifyTime: 18:05
 * Description:
 */
@Route(path = Constants.Router.ChargingTone.MAIN)
class ChargingTonePage : BaseActivity() {
    override fun getLayout(): Int = R.layout.page_charging_tone
    lateinit var mFundViewModel: FundationViewModel
    override fun enableTitleBar(): Boolean = false
    override fun initViewAndData() {
        mFundViewModel = ViewModelProviders.of(this).get(FundationViewModel::class.java)
        mFundViewModel.mCodeStacksRet.observe(this, Observer {
        })
    }

    private fun copyDatabase() {
        AssetsUtils.copyAssetFile(this,
                FundAppDatabase.DB_PATH.replace("fund.db", "")
                , "fund.db")
    }

    override fun initListener() {
        super.initListener()
        setListener(tvTitleSearch)
        etInput.setOnEditorActionListener { _, actionId, event ->
            //当actionId == XX_SEND 或者 XX_DONE时都触发
            //或者event.getKeyCode == ENTER 且 event.getAction == ACTION_DOWN时也触发
            //注意，这是一定要判断event != null。因为在某些输入法上会返回null。
            if (actionId == EditorInfo.IME_ACTION_SEND ||
                    actionId == EditorInfo.IME_ACTION_DONE ||
                    event != null &&
                    KeyEvent.KEYCODE_ENTER == event.keyCode &&
                    KeyEvent.ACTION_DOWN == event.action) { //处理事件
                startSearch()
            }
            false
        }
    }

    override fun onClick(v: View?) {
        super.onClick(v)
        when (v) {
            tvTitleSearch -> {
                startSearch()
            }
        }
    }

    private fun startSearch() {
        val inputMsg = etInput.text.toString().trim()
        if (inputMsg.isEmpty()) {
            shortToast(getString(R.string.hint_search_fund_stocks))
        } else {
            mFundViewModel.searchFundHoldingStocks(inputMsg)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
    }
}